from django.db.models import Q
from django.shortcuts import render
from course import models, courseser
from rest_framework.generics import ListAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination
from django.core.cache import cache


# Create your views here.

class HomeCate(APIView):
    def get(self, request):
        catelist = models.CategoryModel.objects.filter(recommend=True, parent=0)
        ser = courseser.CategorySerializer(catelist, many=True)
        data = ser.data
        for item in data:
            item['son'] = item["son"][0:3]
        return Response({"msg": "ok", "data": ser.data})


class CateCourse(APIView):
    def get(self, request):
        parentid = request.query_params.get("pid")
        catelist = models.CategoryModel.objects.filter(id=parentid).first()
        ser = courseser.CategorySerializer(catelist)
        sonlist = []
        for item in ser.data["son"]:
            sonlist.append(item["id"])
        homecourse = models.CourseModel.objects.filter(category__in=sonlist).order_by('order')[:4]
        coursedata = courseser.CourseSerializer(homecourse, many=True).data
        datalist = ser.data
        datalist["course"] = coursedata
        return Response({"msg": "ok", "data": datalist})


class GetParentCate(APIView):
    def get(self, request):
        parentcate = models.CategoryModel.objects.filter(parent=0)
        ser = courseser.CategorySerializer(parentcate, many=True)
        return Response({"msg": "ok", "data": ser.data})


class GetSonCate(APIView):
    def get(self, request):
        parentid = request.query_params.get("pid")
        if parentid:
            soncate = models.CategoryModel.objects.filter(parent=parentid)
        else:
            soncate = models.CategoryModel.objects.filter(parent__gt=0)
        ser = courseser.CategorySerializer(soncate, many=True)
        return Response({"msg": "ok", "data": ser.data})


class CoursePage(PageNumberPagination):
    page_size = 2
    page_size_query_param = 'size'
    page_query_param = 'page'


class GetCourse(ListAPIView):
    serializer_class = courseser.CourseSerializer
    pagination_class = CoursePage

    def get_queryset(self):
        query = Q(isshow=True)
        # queryset = models.CourseModel.objects.filter(isshow=True).order_by('order')
        cateid = self.request.query_params.get("cateid")
        directionid = self.request.query_params.get("directionid")
        level = self.request.query_params.get("level")
        orderdata = self.request.query_params.get("ordering")
        if level:
            query = query & Q(level=level)
        if cateid:
            query = query & Q(category=cateid)
        else:
            if directionid:
                son = models.CategoryModel.objects.filter(parent=directionid)
                sondata = []
                for i in son:
                    sondata.append(i.id)
                query = query & Q(category__in=sondata)
        if orderdata:
            queryset = models.CourseModel.objects.filter(query).order_by('order', orderdata)
        else:
            queryset = models.CourseModel.objects.filter(query).order_by('order')
        return queryset


class GetDetail(APIView):
    def get(self, request):
        id = request.query_params.get("id")
        data = models.CourseModel.objects.filter(id=id).first()
        ser = courseser.CourseSerializer(data)
        return Response({"msg": "ok", "data": ser.data})
